using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Unity;
using Shatkotha.WPF.Global.Bases;
using Shatkotha.WPF.Global.Data;
using Shatkotha.WPF.Global.Modules.SMS;
using Shatkotha.WPF.Global.Common.Controls.Dialog;
using Shatkotha.WPF.Global.Data.BLL;
using Shatkotha.WPF.SMS.Views;

namespace Shatkotha.WPF.SMS.Presenters
{
    public class NewSmsWizardPresenter:ADialogContentPresenter,INewSmsWizardPresenter
    {
        public DelegateCommand<object> AddCommand { get; set; }
        public DelegateCommand<object> AddAllCommand { get; set; }
        public DelegateCommand<object> RemoveCommand { get; set; }
        public DelegateCommand<object> RemoveAllCommand { get; set; }

        public ObservableCollection<IPerson> PersonCollection { get; set; }
        public ObservableCollection<IPerson> SelectedPersonCollection { get; set; }

        public NewSmsWizardPresenter(INewSmsWizardView view,IUnityContainer container) : base(view)
        {
            AddCommand = new DelegateCommand<object>(ExecuteAddCommand);
            var personManager = container.Resolve<IPersonManager>();
            PersonCollection = personManager.GetAllPerson();
            SelectedPersonCollection = new ObservableCollection<IPerson>();
        }

        private void ExecuteAddCommand(object obj)
        {
            var selectedPerson = (IPerson) ((NewSmsWizardView) View).ListBoxPersons.SelectedItem;
            if(selectedPerson!=null)
            {
                PersonCollection.Remove(selectedPerson);
                SelectedPersonCollection.Add(selectedPerson);
            }
        }

        public IPerson[] Recipients
        {
            get
            {
                return SelectedPersonCollection.ToArray();
            }
            set
            {
                foreach (var person in value)
                {
                    SelectedPersonCollection.Add(person);    
                }
            }
        }

        public string MessageText
        {
            get; 
            set;
        }
    }
}